#include "imsecuritycodedlg.h"
#include "ui_imsecuritycodedlg.h"
#include "../Biz/ConfigurationMgr.hpp"
#include <QShowEvent>
#include <QMessageBox>

IMSecurityCodeDlg::IMSecurityCodeDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IMSecurityCodeDlg)
{
    ui->setupUi(this);
    QObject::connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onCheckAuthCode()));
    QObject::connect(this, SIGNAL(rejected()), SLOT(onCancel()));
}

IMSecurityCodeDlg::~IMSecurityCodeDlg()
{
    delete ui;
}

void IMSecurityCodeDlg::SetAuthCodeSession(const std::string &session)
{
    m_session = session;
}

void IMSecurityCodeDlg::SetAuthStateInfo(const QString &info)
{
    ui->err_info->setText(info);
    ui->webView->reload();
}

void IMSecurityCodeDlg::showEvent(QShowEvent *event)
{
    QString sessionstr = QString::fromStdString(m_session);
    std::string authurl;
    IMGet_BizConfig("securitycode.url", authurl);
    QUrl aucode_url =  QUrl(QString::fromStdString(authurl) + sessionstr);
    ui->webView->setUrl(aucode_url);
    ui->webView->load(aucode_url);
    ui->webView->show();

    event->accept();
}

void IMSecurityCodeDlg::onCheckAuthCode()
{
    if(ui->authcode_inputer->text().size() <= 3)
    {
        ui->err_info->setText("wrong input.");
        return;
    }
    ui->err_info->setText("authing, please wait ....");
    emit sendAuthCode(m_session, ui->authcode_inputer->text().toStdString());
}
void IMSecurityCodeDlg::onCancel()
{
    emit cancelAuthCode(m_session);
}
